<?php
$db = new myDBS;
$db->selectDB("account");

$tblname = $_REQUEST["tblname"];

if ($tblname == "")
    $tblname = "tbl_pet";
?>
<script type="text/javascript">
    $(document).ready(function() {
        $('#petition').dataTable();

        $("#tblname").change(function() {
            //alert($(this).val());
            window.location.href = "index.php?pages=cheque_manage&tblname=" + $(this).val();
        });
    });

    function paidCheque(cheque_no) {
        //var task = "paid";

        $('<div>').dialog2({
            autoOpen: true,
            title: "จ่ายเช็ค",
            content: "dlg_cheque_receiver.php?cheque_no=" + cheque_no + "&tblname=tbl_pet",
            id: "modal-dlg"
        });
    }

    function pause(cheque_no, pause_status) {
        var task = "pause";
        var tblname = "<?php echo $tblname; ?>";
        //alert(tblname);
        $.post('cheque_process.php', {task: task, cheque_no: cheque_no, pause_status: pause_status, tblname: tblname}, function(data) {
            window.location.href = "index.php?pages=cheque_manage&tblname=" + tblname;
        });

    }

    function approvedCheque(cheque_no) {
        var task = "approvedCheque";
        var tblname = "<?php echo $tblname; ?>";

        $('<div>').dialog2({
            autoOpen: true,
            title: "อนุมัติ",
            content: "dlgDate_1.php?cheque_no=" + cheque_no + "&task=" + task + "&tblname=" + tblname,
            id: "modal-dlg"
        });
    }

    function cancelCheque(cheque_no) {

        $('<div>').dialog2({
            autoOpen: true,
            title: "ยกเลิก",
            content: "dlg_cheque_cancel.php?cheque_no=" + cheque_no + "&tblname=tbl_pet",
            id: "modal-dlg"
        });

    }

</script>

<?php
//require_once("includes/connect.php");

switch ($tblname) {
    case "tbl_pet":
        $sql = "SELECT DISTINCT tbl_pay.cheque_no , bank.bank_name, bank_branch.branch_name, "
                . " credit_name AS receiver, status, tbl_pay.amount AS net , cheque_status, paid_status, pause_status"
                . " FROM tbl_creditor, tbl_pet, bank, bank_branch,tbl_pay "
                . " WHERE tbl_pet.status in( 'จัดทำเช็ค', 'เช็คอนุมัติ', 'จ่ายเช็คแล้ว') "
                . " AND tbl_pet.receiver = tbl_creditor.creditor_id "
                . " AND tbl_pet.pet_id = tbl_pay.pet_id "
                . " AND tbl_pay.bank_id = bank.bank_id "
                . " AND tbl_pay.bank_branch_id = bank_branch.bank_branch_id "
                . " ORDER BY cheque_no , bank_name, branch_name, credit_name";
//        $sql = "SELECT  cheque_no , bank_name, branch_name, receiver, cheque_status, paid_status"
//                . " , pause_status, SUM(net) AS net "
//                . " FROM  tbl_pet "
//                . " WHERE approved_status =  'อนุมัติ' "
//                . " AND cheque_no <> '' "
//                . " GROUP BY  cheque_no , bank_name, branch_name, receiver, cheque_status, paid_status, pause_status "
//                . " ORDER BY  cheque_no";

        break;

    case "tbl_petition":

        $sql = "SELECT  cheque_no , bank_name, branch_name, company_name AS receiver, cheque_status"
                . " , paid_status, pause_status, SUM(inpNet) AS net "
                . " FROM  tbl_petition "
                . " WHERE approved_status =  'อนุมัติ' "
                . " AND cheque_no <> '' "
                . " GROUP BY  cheque_no , bank_name, branch_name, receiver, cheque_status, paid_status, pause_status "
                . " ORDER BY  cheque_no";
        break;

    case "tbl_salary":

        $sql = "SELECT  cheque_no , bank_name, branch_name, receiver, cheque_status"
                . " , paid_status, pause_status, SUM(salary) AS net "
                . " FROM  tbl_salary "
                . " WHERE approved_status =  'อนุมัติ' "
                . " AND cheque_no <> '' "
                . " GROUP BY  cheque_no , bank_name, branch_name, receiver, cheque_status, paid_status, pause_status "
                . " ORDER BY  cheque_no";
        break;

    case "tbl_welfare":

        $sql = "SELECT  cheque_no , bank_name, branch_name, receiver, cheque_status"
                . " , paid_status, pause_status, SUM(welfare) AS net "
//                . " , paid_status, pause_status, welfare AS net "
                . " FROM  tbl_welfare "
                . " WHERE approved_status =  'อนุมัติ' "
                . " AND cheque_no <> '' "
                . " GROUP BY  cheque_no , bank_name, branch_name, receiver, cheque_status, paid_status, pause_status "
                . " ORDER BY  cheque_no";
        break;
}


//echo $sql;
$result = $db->runSQL($sql);
?>
<div class="control-group">
    <div class="controls-row">
        <select id="tblname" name="tblname" >
            <option value="">เลือก...</option>
            <option value="tbl_pet" <?php echo ($tblname == "tbl_pet") ? "selected" : ""; ?>>อื่นๆ</option>
            <option value="tbl_petition" <?php echo ($tblname == "tbl_petition") ? "selected" : ""; ?>>โครงการ</option>
            <option value="tbl_salary" <?php echo ($tblname == "tbl_salary") ? "selected" : ""; ?>>เงินเดือน</option>
            <option value="tbl_welfare" <?php echo ($tblname == "tbl_welfare") ? "selected" : ""; ?>>สวัสดิการ</option>
        </select>

    </div>
</div>
<table id="petition" class="table table-bordered table-hover" width="100%" >
    <caption><br></caption>
    <thead>
        <tr>
            <th>#</th>
            <th>เลขที่เช็ค</th>
            <th>ชื่อธนาคาร</th>
            <th>สาขา</th>
            <th>ผู้รับเงิน</th>
            <th>จำนวนเงิน</th>
            <th>อนุมัติเช็ค</th>
            <th>พิมพ์เช็ค</th>
            <th>พักเช็ค</th>
            <th>จ่ายเช็ค</th>
        </tr>
    </thead>
    <tbody>
        <?php
        $i = 0;
        while ($rows = $db->fetch_object($result)) {
            $i++;
            ?>
            <tr <?php echo ($rows->cheque_status == "ยกเลิก") ? "class='error'" : ""; ?>>
                <td><?php echo $i; ?></td>
                <td><?php echo $rows->cheque_no; ?></td>
                <td><?php echo $rows->bank_name; ?></td>
                <td><?php echo $rows->branch_name; ?></td>
                <td><?php echo $rows->receiver; ?></td>
                <td><p class="text-right"><?php echo number_format($rows->net, 2); ?></p></td>
                <td>
                    <p class="text-center">
                        <?php
                        if ($rows->cheque_status == "รออนุมัติ") {
                            echo "<a href='javascript:approvedCheque(\"$rows->cheque_no\")' data-toggle='tooltip' title='อนุมัติเช็ค'><i class='icon-ok'></i></a>&nbsp;<a href='javascript:cancelCheque(\"$rows->cheque_no\")' data-toggle='tooltip' title='ยกเลิกเช็ค'><i class='icon-remove'></i></a>
							";
                        } else {
                            echo $rows->cheque_status;
                        }
                        ?>
                    </p>
                </td>
                <td>
                    <p class="text-center">
                        <?php
                        if ($rows->cheque_status <> "ยกเลิก") {
                            ?>
                            <a href="cheque_print.php?cheque_no=<?php echo $rows->cheque_no; ?>&tblname=<?php echo $tblname; ?>" target="_blank"><i class='icon-print'></i></a>
                            <?php
                        }
                        ?>
                    </p>
                </td>
                <td><p class="text-center">
                        <?php
                        if ($rows->cheque_status <> "ยกเลิก") {
                            if ($rows->pause_status == "พักเช็คชั่วคราว") {
                                ?>
                                <a href="javascript:pause('<?php echo $rows->cheque_no; ?>', 'ปกติ')" title="ปกติ"><i class="icon-play"></i></a>

                                <?php
                            } else {
                                ?>
                                <a href="javascript:pause('<?php echo $rows->cheque_no; ?>', 'พักเช็คชั่วคราว')" title="พักเช็คชั่วคราว"><i class="icon-pause"></i></a>

                                <?php
                            }
                        }
                        ?>
                    </p>
                </td>
                <td><p class="text-center">
                        <?php
                        if ($rows->cheque_status <> "ยกเลิก") {
                            if ($rows->cheque_status == "อนุมัติ" && $rows->pause_status == "ปกติ") {
                                if ($rows->paid_status == "รอจ่าย") {
                                    echo "<a href='javascript:paidCheque(\"$rows->cheque_no\")' data-toggle='tooltip' title='จ่ายเช็ค'><i class='icon-ok'></i></a>";
                                } else {
                                    echo $rows->paid_status;
                                }
                            } else {
                                echo "ยังไม่อนุมัติ หรือ พักเช็คชั่วคราว";
                            }
                        }
                        ?>
                    </p>
                </td>

            </tr>
            <?php
        }
        ?>

    </tbody>


</table>
